package com.spring.sso.config.security.strategy;

import com.spring.sso.common.enums.AuthChannelEnum;
import com.spring.sso.common.enums.AuthEndpointEnum;
import com.spring.sso.config.common.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @author : pangfuzhong
 * @description
 * @date : 2021/9/29 12:02
 */
public abstract class AbstractLoginAuthFilterStrategy extends OncePerRequestFilter implements AuthFilterStrategy {

    @Override
    public Boolean match(HttpServletRequest request, HttpServletResponse response) {
        // 登陆方式
        String endPoint = request.getHeader(Constant.ENDPOINT_HEADER);
        if(StringUtils.isBlank(endPoint)) {
            return Boolean.FALSE;
        }

        // 登陆渠道
        String channel = request.getHeader(Constant.CHANNEL_HEADER);
        if(StringUtils.isBlank(endPoint)) {
            return Boolean.FALSE;
        }

        // 登陆方式合法性
        AuthEndpointEnum authEndpointEnum = AuthEndpointEnum.getByCode(endPoint);
        if(Objects.isNull(authEndpointEnum)) {
            return Boolean.FALSE;
        }

        // 登陆渠道合法性
        AuthChannelEnum authChannelEnum = AuthChannelEnum.getByCode(channel);
        if(Objects.isNull(authChannelEnum)) {
            return Boolean.FALSE;
        }

        // 是否匹配当前过滤器处理
        if(!(authEndpointEnum.equals(this.getAuthEndpointEnum()) && authChannelEnum.equals(this.getAuthEndpointEnum()))) {
            return Boolean.FALSE;
        }

        return Boolean.TRUE;
    }
}
